import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, accuracy_score
from sklearn.datasets import fetch_openml
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from joblib import dump, load

# 加载MNIST数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# 数据预处理，将图像数据转换为浮点数并归一化
train_images = train_images.reshape((train_images.shape[0], -1)).astype('float32') / 255
test_images = test_images.reshape((test_images.shape[0], -1)).astype('float32') / 255

# 划分训练集和测试集
X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.1, random_state=42)

# 数据归一化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)
test_images = scaler.transform(test_images)

# 创建KNN分类器
knn = KNeighborsClassifier(n_neighbors=3)

# 训练KNN分类器
knn.fit(X_train, y_train)

# 在验证集上进行预测
y_val_pred = knn.predict(X_val)

# 打印验证集上的分类报告和准确率
print(classification_report(y_val, y_val_pred))
print("Validation Accuracy:", accuracy_score(y_val, y_val_pred))

# 在测试集上进行预测
y_test_pred = knn.predict(test_images)

# 打印测试集上的分类报告和准确率
print(classification_report(test_labels, y_test_pred))
print("Test Accuracy:", accuracy_score(test_labels, y_test_pred))

dump(knn, 'knn_mnist_model.joblib')
dump(scaler, 'scaler.joblib')  # 保存标准化对象